package com.slicktechnologies.client.views.settings.employee;
import java.util.Date;

import com.google.gwt.cell.client.ButtonCell;
import com.google.gwt.cell.client.DatePickerCell;
import com.google.gwt.cell.client.FieldUpdater;
import com.google.gwt.cell.client.TextCell;
import com.google.gwt.cell.client.TextInputCell;
import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.i18n.client.DateTimeFormat.PredefinedFormat;
import com.google.gwt.user.cellview.client.Column;
import com.google.gwt.user.cellview.client.HasKeyboardSelectionPolicy.KeyboardSelectionPolicy;
import com.google.gwt.user.cellview.client.TextColumn;
import com.google.gwt.view.client.ProvidesKey;
import com.simplesoftwares.client.library.appstructure.SuperTable;
import com.simplesoftwares.client.library.appstructure.UiScreen;
import com.slicktechnologies.client.utility.AppUtility;
import com.slicktechnologies.shared.common.helperlayer.EducationalInfo;
import com.slicktechnologies.shared.common.helperlayer.PreviousCompanyHistory;


public class PreviousWorkExperienceTable extends SuperTable<PreviousCompanyHistory>  
{
	Column<PreviousCompanyHistory,String> deleteColumn;
	Column<PreviousCompanyHistory,String> companyColumn,designationColumn,salaryColumn;
	Column<PreviousCompanyHistory,Date>fromDateColumn,toDateColumn;
	TextColumn<PreviousCompanyHistory> viewcompanyColumn,viewDesignationColumn,viewSalaryColumn,
	viewfromDate,viewToDate;



	public PreviousWorkExperienceTable()
	{
		super();
	}
	public PreviousWorkExperienceTable(UiScreen<PreviousCompanyHistory> view)
	{
		super(view);	
	}

	public void createTable()
	{
		
		createColumnlnCompany();
		createColumnlnDesignation();
		createColumnlnSalary();
		createColumnlnFromDate();
		createColumnlnToDate();
		
		createColumndeleteColumn();
		createViewCompanyColumn();
		createViewDesignationColumn();
		createViewSalaryColumn();
		createViewfromDate();
		createViewToDate();
		table.addColumn(companyColumn,"Company");
    	table.addColumn(designationColumn,"Designation");
    	table.addColumn(salaryColumn,"Salary");
    	table.addColumn(fromDateColumn,"From");
    	table.addColumn(toDateColumn,"To");
    	
    	table.addColumn(deleteColumn,"Remove");
    	table.setLoadingIndicator(null);
    	
    	addFieldUpdater();
    	table.setLoadingIndicator(null);
		table.setKeyboardSelectionPolicy(KeyboardSelectionPolicy.DISABLED);	
		
	}
	
	protected void createViewCompanyColumn()
	{
		
		viewcompanyColumn=new TextColumn<PreviousCompanyHistory>()
				{
			@Override
			public String getValue(PreviousCompanyHistory object)
			{
				return object.getCompany();
			}
				};
			
	}
	
	
	protected void createViewDesignationColumn()
	{
		
		viewDesignationColumn=new TextColumn<PreviousCompanyHistory>()
				{
			@Override
			public String getValue(PreviousCompanyHistory object)
			{
				return object.getDesignation();
			}
				};
				
	}
	
	protected void createViewSalaryColumn()
	{
		
		viewSalaryColumn=new TextColumn<PreviousCompanyHistory>()
				{
			@Override
			public String getValue(PreviousCompanyHistory object)
			{
				return object.getSalary()+"";
			}
				};
				
	}
	
	protected void createViewfromDate()
	{
		
		viewfromDate=new TextColumn<PreviousCompanyHistory>()
				{
			@Override
			public String getValue(PreviousCompanyHistory object)
			{
				return AppUtility.parseDate(object.getFromDate());
			}
				};
				
	}
	
	
	protected void createViewToDate()
	{
		
		viewToDate=new TextColumn<PreviousCompanyHistory>()
				{
			@Override
			public String getValue(PreviousCompanyHistory object)
			{
				return AppUtility.parseDate(object.getToDate());
			}
				};
				
	}
	
	
	
			
	
	
	
	//************************************************************************************************************//	
	
	
	
	protected void createColumnlnCompany()
	{
		TextInputCell editCell=new TextInputCell();
		companyColumn=new Column<PreviousCompanyHistory,String>(editCell)
				{
			@Override
			public String getValue(PreviousCompanyHistory object)
			{
				return object.getCompany();
			}
				};
				
	}
	
	protected void createColumnlnDesignation()
	{
		TextInputCell editCell=new TextInputCell();
		designationColumn=new Column<PreviousCompanyHistory,String>(editCell)
				{
			@Override
			public String getValue(PreviousCompanyHistory object)
			{
				return object.getDesignation();
			}
				};
				
	}
	
	
	protected void createColumnlnSalary()
	{
		TextInputCell editCell=new TextInputCell();
		salaryColumn=new Column<PreviousCompanyHistory,String>(editCell)
				{
			@Override
			public String getValue(PreviousCompanyHistory object)
			{
				return object.getSalary()+"";
			}
				};
				
	}
	
	protected void createColumnlnFromDate()
	{
		DateTimeFormat fmt = DateTimeFormat.getFormat(PredefinedFormat.DATE_SHORT);
		DatePickerCell date= new DatePickerCell(fmt);
		
		fromDateColumn=new Column<PreviousCompanyHistory,Date>(date)
				{
			@Override
			public Date getValue(PreviousCompanyHistory object)
			{
				return object.getFromDate();
			}
				};
				
	}
	
	
	protected void createColumnlnToDate()
	{
		DateTimeFormat fmt = DateTimeFormat.getFormat(PredefinedFormat.DATE_SHORT);
		DatePickerCell date= new DatePickerCell(fmt);
		
		toDateColumn=new Column<PreviousCompanyHistory,Date>(date)
				{
			@Override
			public Date getValue(PreviousCompanyHistory object)
			{
				return object.getToDate();
			}
				};
				
	}
	
	
	
	protected void createColumndeleteColumn()
	{
		ButtonCell btnCell= new ButtonCell();
		deleteColumn=new Column<PreviousCompanyHistory,String>(btnCell)
				{
			@Override
			public String getValue(PreviousCompanyHistory object)
			{
				return  "Delete" ;
			}
				};
				
	}

//************************************************************************************************************//

	@Override public void addFieldUpdater() 
	{
		createFieldUpdaterCompany();
		createFieldUpdaterDesignation();
		createFieldUpdaterSalary();
		createFieldUpdaterdeleteColumn();
	}

	protected void createFieldUpdaterCompany()
	{
		companyColumn.setFieldUpdater(new FieldUpdater<PreviousCompanyHistory,String>()
				{
			@Override
			public void update(int index,PreviousCompanyHistory object,String value)
			{
				if(value!=null)
				{
					if(value.trim().equals("")==false)
						object.setCompany(value);
				}

				table.redrawRow(index);
			}
				});
	}
	
	
	protected void createFieldUpdaterDesignation()
	{
		designationColumn.setFieldUpdater(new FieldUpdater<PreviousCompanyHistory,String>()
				{
			@Override
			public void update(int index,PreviousCompanyHistory object,String value)
			{
				if(value!=null)
				{
					if(value.trim().equals("")==false)
						object.setDesignation(value);
				}

				table.redrawRow(index);
			}
				});
	}
	
	
	protected void createFieldUpdaterSalary()
	{
		salaryColumn.setFieldUpdater(new FieldUpdater<PreviousCompanyHistory,String>()
				{
			@Override
			public void update(int index,PreviousCompanyHistory object,String value)
			{
				if(value!=null)
				{
					if(value.trim().equals("")==false)
					{
						try{
							Double salary=Double.parseDouble(value.trim());
							object.setSalary(salary);
							
						}
						catch(Exception e)
						{
							
						}
					}
				}

				table.redrawRow(index);
			}
				});
	}
	
	protected void createFieldUpdaterFromDate()
	{
		fromDateColumn.setFieldUpdater(new FieldUpdater<PreviousCompanyHistory,Date>()
				{
			@Override
			public void update(int index,PreviousCompanyHistory object,Date date)
			{
				if(date!=null)
					object.setFromDate(date);

				table.redrawRow(index);
			}
			});
	}
	
	
	protected void createFieldUpdaterToDate()
	{
		toDateColumn.setFieldUpdater(new FieldUpdater<PreviousCompanyHistory,Date>()
				{
			@Override
			public void update(int index,PreviousCompanyHistory object,Date date)
			{
				if(date!=null)
					object.setToDate(date);

				table.redrawRow(index);
			}
			});
	}
	
	


	


	
	protected void createFieldUpdaterdeleteColumn()
	{
		deleteColumn.setFieldUpdater(new FieldUpdater<PreviousCompanyHistory,String>()
				{
			@Override
			public void update(int index,PreviousCompanyHistory object,String value)
			{
				getDataprovider().getList().remove(object);

				table.redrawRow(index);
			}
				});
	}


	public void addColumnSorting(){

		

	}

	
	@Override
	public void setEnable(boolean state)
	{
          
		
        for(int i=table.getColumnCount()-1;i>-1;i--)
    	  table.removeColumn(i); 
        	  
        System.out.println("Called Set Enabeled With State"+state);
          if(state==true)
          {
        	table.addColumn(companyColumn,"Company");
        	table.addColumn(designationColumn,"Designation");
        	table.addColumn(salaryColumn,"Salary");
        	table.addColumn(fromDateColumn,"From");
        	table.addColumn(toDateColumn,"To");
        	table.addColumn(deleteColumn,"Remove");
        	addFieldUpdater();
        	
      	}
          
          
          else
          {
        	  
        	table.addColumn(viewcompanyColumn,"Company");
          	table.addColumn(viewDesignationColumn,"Designation");
          	table.addColumn(viewSalaryColumn,"Salary");
          	table.addColumn(viewfromDate,"From");
        	table.addColumn(viewToDate,"To");
          	
        	
      	
          }
	}

	@Override
	public void applyStyle()
	{

	}
	@Override
	protected void initializekeyprovider() {
		keyProvider= new ProvidesKey<PreviousCompanyHistory>()
				{
			@Override
			public Object getKey(PreviousCompanyHistory item)
			{
				return new Date().getTime();
			}
				};
	}


	
	
}
